package com.cloudcross.ssp.listener;

import java.util.HashMap;
import java.util.Map;

/**
 * 初始化一些数据，让项目一启动就加载进内存
 * @author 杜武强
 *
 */
public class InitData {
	private static InitData instance;
	
	//用来存储地域的location,cn,cn_city进内存,这里以location为key,value是"cn,cn_city"
	public static Map<String, String> locationMap = new HashMap<String, String>();
	//这里存储连接mongoDB数据库信息
	public static Map<String, Object> mongodbMap = new HashMap<String, Object>();
	
	private InitData() {
		
	}
	
	public InitData getInitDataInstance() {
		if(null == instance) {
			instance = new InitData();
		}
		
		return instance;
	}
	/**
	 * 根据location 获取cn和cncity
	 * @param location
	 * @return
	 */
	public static String getcnAndCncity(String location) {
		for(Map.Entry<String, String> map : locationMap.entrySet()) {
			if(map.getKey().equals(location)) {
				return map.getValue();
			}
		}
		
		return null;
	}
	
	/**
	 * 根据省得到location,没有则返回空值
	 * @param cn
	 * @return
	 */
	public static String getLocation(String cn) {
		for(Map.Entry<String, String> map : locationMap.entrySet()) {
			if(map.getValue().equals(cn)) {
				return map.getKey();
			}
		}
		
		return null;
	}
	
	
	/**
	 * 根据省和市得到location,没有则返回空值
	 * @param cn
	 * @return
	 */
	public static String getLocation(String cn, String cnCity) {
		for(Map.Entry<String, String> map : locationMap.entrySet()) {
			if(map.getValue().equals(cn + "," + cnCity)) {
				return map.getKey();
			}
		}
		
		return null;
	}
	
	/**
	 * 根据key获取value
	 * @param
	 * @return
	 */
	public static Object getMongoDBInfo(String key) {
		for(Map.Entry<String, Object> map : mongodbMap.entrySet()) {
			if(map.getKey().equals(key)) {
				return mongodbMap.get(key);
			}
		}
		return null;
	}
}
